package com.settlement.system.model.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.settlement.system.common.base.BaseEntity;
import com.settlement.system.common.util.StringUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 京东资金流水
 * </p>
 *
 * @author huangrensen
 * @since 2024-05-17
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class JdFundFlow extends BaseEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * ID
     */
    @TableId(value = "id")
    private String id;


    /**
     * 店铺ID
     */
    private Long storeId;
    @TableField(exist = false)
    private String storeName;


    /**
     * 商户号
     */
    private String sellerNo;


    /**
     * 账户代码
     */
    private String accountCode;


    /**
     * 账户名称
     */
    private String accountName;


    /**
     * 日期
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime billTime;


    /**
     * 商户订单号
     */
    private String sellerOrderNo;
    @TableField(exist = false)
    private List<String> sellerOrderNoList;


    /**
     * 账户余额(元)
     */
    private BigDecimal balance;


    /**
     * 收入金额(元)
     */
    private BigDecimal incomeAmount;


    /**
     * 支出金额(元)
     */
    private BigDecimal payAmount;


    /**
     * 资金动账备注
     */
    private String remark;


    /**
     * 交易类型
     */
    private String billType;


    /**
     * 账单日期
     */
    private LocalDate billDate;

    @TableField(exist = false)
    private String tableName;
    @TableField(exist = false)
    private String billMonth;
    @TableField(exist = false)
    private LocalDateTime startDate;
    @TableField(exist = false)
    private LocalDateTime endDate;

    private static boolean compareDate(LocalDate date1, LocalDate date2) {
        if (date1 == null && date2 == null) {
            return true;
        }
        if (date1 != null && date1.equals(date2)) {
            return true;
        }
        return false;
    }

    private static boolean compareDate(LocalDateTime date1, LocalDateTime date2) {
        if (date1 == null && date2 == null) {
            return true;
        }
        if (date1 != null && date2 == null) {
            return false;
        }
        return date1 != null && date1.equals(date2);
    }

    private static boolean compareBigDecimal(BigDecimal value1, BigDecimal value2) {
        if (value1 == null && value2 == null) {
            return true;
        }
        if (Objects.isNull(value2)) {
            value2 = BigDecimal.ZERO;
        }
        return value1 != null && value1.compareTo(value2) == 0;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        JdFundFlow that = (JdFundFlow) o;
        return Objects.equals(storeId, that.storeId) && Objects.equals(sellerNo, that.sellerNo) && Objects.equals(accountCode, that.accountCode) && Objects.equals(accountName, that.accountName) && compareDate(billTime, that.billTime) && Objects.equals(sellerOrderNo, that.sellerOrderNo) && compareBigDecimal(balance, that.balance) && compareBigDecimal(incomeAmount, that.incomeAmount) && compareBigDecimal(payAmount, that.payAmount) && Objects.equals(remark, that.remark) && Objects.equals(billType, that.billType) && compareDate(billDate, that.billDate);
    }

    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), storeId, sellerNo, accountCode, accountName, billTime, sellerOrderNo, balance, incomeAmount, payAmount, remark, billType, billDate);
    }

    public void formatTradeType() {
        if (!StringUtils.hasText(remark)) {
            return;
        }
        if (remark.contains("redpack抢红包")) {
            this.billType = "redpack抢红包";
        } else if (remark.contains("代收付服务费")) {
            this.billType = "代收付服务费";
        } else if (remark.contains("货款") || remark.contains("退货") || remark.contains("退款")) {
            this.billType = "货款";
        } else if (remark.replaceAll("（", "(").replaceAll("）", ")").contains("京仓结算(新)配送服务")) {
            this.billType = "京仓结算(新)配送服务";
        } else if (remark.contains("京挑客扣款")) {
            this.billType = "京挑客";
        } else if (remark.contains("商务舱广告销售")) {
            this.billType = "商务舱广告销售";
        } else if (remark.contains("企业站提现")) {
            this.billType = "提现";
        } else if (remark.contains("费项")) {
            this.billType = "佣金";
        } else if (remark.contains("安联保险费")) {
            this.billType = "运费险";
        }
    }

    // 截取长度超出范围的字段
    public void cutString() {
        if (this.remark != null && this.remark.length() > 2000) {
            this.remark = this.remark.substring(0, 2000);
        }
    }
}
